package com.ssy.lingxi.marketing.serviceimpl.component;

import com.ssy.lingxi.common.constant.member.AbilitySuitableMemberEnum;
import com.ssy.lingxi.common.constant.member.MemberTypeEnum;
import com.ssy.lingxi.common.constant.member.PlatformSuitableMemberEnum;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 适用会员字段处理组件
 * 优惠券与活动处理的方式不一样
 * 优惠券提交的数据为list, 活动提交的数据为每个字段的布尔值
 * 由于代码是接手的, 接口已经跟前端对接好了, 所以活动的字段就按照原来的方式返回, 没有改动
 * @author yzc
 * @version 2.0.0
 * @date 2021/9/17
 */
public class SuitableFieldComponent {

    /**
     * pf - platform 平台后台(平台)
     * at - ability 能力中心(商家)
     * loc - location 位置
     *
     */
    public static final Integer zero_char_mark = 0;
    public static final Integer one_char_mark = 1;

    public static final Integer pf_new_member_loc = 1;
    public static final Integer pf_ole_member_loc = 2;

    public static final Integer pf_merchant_type_loc = 1;
    public static final Integer pf_merchant_personal_type_loc = 2;

    public static final Integer at_new_user_loc = 1;
    public static final Integer at_ole_user_loc = 2;
    public static final Integer at_new_member_loc = 3;
    public static final Integer at_old_member_loc = 4;

    private SuitableFieldComponent() {
    }

    // ==================================优惠券==================================

    /**
     * 生成商家活动适用用户标记
     * @return 返回结果
     */
    public static String generateCouponSuitableUser(List<Integer> suitableUserList) {
        Integer mNewUser = suitableUserList.contains(AbilitySuitableMemberEnum.NEW_USER.getCode()) ? one_char_mark : zero_char_mark;
        Integer mOldUser = suitableUserList.contains(AbilitySuitableMemberEnum.OLD_USER.getCode()) ? one_char_mark : zero_char_mark;
        Integer mNewMember = suitableUserList.contains(AbilitySuitableMemberEnum.NEW_MEMBER.getCode()) ? one_char_mark : zero_char_mark;
        Integer mOldMember = suitableUserList.contains(AbilitySuitableMemberEnum.OLD_MEMBER.getCode()) ? one_char_mark : zero_char_mark;
        return generateFieldLocation(mNewUser, mOldUser, mNewMember, mOldMember);
    }

    /**
     * 解析商家活动适用用户标记
     * @param suitableUser 适用用户标记
     * @return 返回结果
     */
    public static List<Integer> parseCouponSuitableUser(String suitableUser) {
        List<Integer> list = new ArrayList<>();
        for (AbilitySuitableMemberEnum value : AbilitySuitableMemberEnum.values()) {
            if (parseFieldLocation(suitableUser, value.getCode())) {
                list.add(value.getCode());
            }
        }
        return list;
    }

    /**
     * 生成平台活动适用会员标记
     * @return 返回结果
     */
    public static String generateCouponSuitableMember(List<Integer> suitableMemberList) {
        Integer pNewMember = suitableMemberList.contains(PlatformSuitableMemberEnum.NEW_PLATFORM_MEMBER.getCode()) ? one_char_mark : zero_char_mark;
        Integer pOldMember = suitableMemberList.contains(PlatformSuitableMemberEnum.OLD_PLATFORM_MEMBER.getCode()) ? one_char_mark : zero_char_mark;
        return generateFieldLocation(pNewMember, pOldMember);
    }

    /**
     * 解析平台活动适用会员标记
     * @param suitableMember 适用会员标记
     * @return 返回结果
     */
    public static List<Integer> parseCouponSuitableMember(String suitableMember) {
        List<Integer> list = new ArrayList<>();
        for (PlatformSuitableMemberEnum value : PlatformSuitableMemberEnum.values()) {
            if (parseFieldLocation(suitableMember, value.getCode())) {
                list.add(value.getCode());
            }
        }
        return list;
    }

    /**
     * 生成平台活动适用会员类型标记
     * @return 返回结果
     */
    public static String generateCouponMemberType(List<Integer> suitableMemberTypeList) {
        Integer pNewMember = suitableMemberTypeList.contains(MemberTypeEnum.MERCHANT.getCode()) ? one_char_mark : zero_char_mark;
        Integer pOldMember = suitableMemberTypeList.contains(MemberTypeEnum.MERCHANT_PERSONAL.getCode()) ? one_char_mark : zero_char_mark;
        return generateFieldLocation(pNewMember, pOldMember);
    }

    /**
     * 解析 平台[活动/优惠券] 适用会员类型标记
     * @param suitableMemberType 适用会员类型标记
     * @return 返回结果
     */
    public static List<Integer> parseMarketingMemberType(String suitableMemberType) {
        List<Integer> list = new ArrayList<>();
        if (parseFieldLocation(suitableMemberType, pf_merchant_type_loc)) {
            list.add(MemberTypeEnum.MERCHANT.getCode());
        }
        if (parseFieldLocation(suitableMemberType, pf_merchant_personal_type_loc)) {
            list.add(MemberTypeEnum.MERCHANT_PERSONAL.getCode());
        }
        return list;
    }

    // ==================================活动==================================

    /**
     * 生成商家活动适用用户标记
     * @param mNewUser 新用户
     * @param mOldUser 老用户
     * @param mNewMember 新会员
     * @param mOldMember 老会员
     * @return 返回结果
     */
    public static String generateActivitySuitableUser(Integer mNewUser, Integer mOldUser, Integer mNewMember, Integer mOldMember) {
        return generateFieldLocation(mNewUser, mOldUser, mNewMember, mOldMember);
    }

    /**
     * 解析商家活动适用用户标记
     * @param suitableUser 适用用户标记
     * @param location 标记位
     * @return 返回结果
     */
    public static Integer parseActivitySuitableUser(String suitableUser, int location) {
        return parseFieldLocation(suitableUser, location) ? one_char_mark : zero_char_mark;
    }

    public static List<Integer> parseActivitySuitableUser(String suitableUser) {
        List<Integer> list = new ArrayList<>();
        for (AbilitySuitableMemberEnum value : AbilitySuitableMemberEnum.values()) {
            if (parseFieldLocation(suitableUser, value.getCode())) {
                list.add(value.getCode());
            }
        }
        return list;
    }

    /**
     * 生成平台活动适用会员标记
     * @param pNewMember 新会员
     * @param pOldMember 老会员
     * @return 返回结果
     */
    public static String generateActivitySuitableMember(Integer pNewMember, Integer pOldMember) {
        return generateFieldLocation(pNewMember, pOldMember);
    }

    /**
     * 解析平台活动适用会员标记
     * @param suitableMember 适用会员标记
     * @param location 标记位
     * @return 返回结果
     */
    public static Integer parseActivitySuitableMember(String suitableMember, int location) {
        return parseFieldLocation(suitableMember, location) ? one_char_mark : zero_char_mark;
    }

    public static List<Integer> parseActivitySuitableMember(String suitableMember) {
        List<Integer> list = new ArrayList<>();
        for (PlatformSuitableMemberEnum value : PlatformSuitableMemberEnum.values()) {
            if (parseFieldLocation(suitableMember, value.getCode())) {
                list.add(value.getCode());
            }
        }
        return list;
    }

    /**
     * 生成平台活动适用会员类型标记
     * @param enterpriseMember 企业会员
     * @param personalMember 个人会员
     * @return 返回结果
     */
    public static String generateActivitySuitableMemberType(Integer enterpriseMember, Integer personalMember) {
        return generateFieldLocation(enterpriseMember, personalMember);
    }

    /**
     * 解析平台活动适用会员类型标记
     * @param suitableMemberType 适用会员类型标记
     * @param location 标记位
     * @return 返回结果
     */
    public static Integer parseActivitySuitableMemberType(String suitableMemberType, int location) {
        return parseFieldLocation(suitableMemberType, location) ? one_char_mark : zero_char_mark;
    }

    /**
     * 根据参数生成标记
     * @param value 参数
     * @return 返回结果
     */
    public static String generateFieldLocation(Integer... value) {
        StringBuilder stringBuilder = new StringBuilder();
        for (Integer integer : value) {
            Integer location = zero_char_mark;
            if (Objects.nonNull(integer) && one_char_mark.equals(integer)) {
                location = one_char_mark;
            }
            stringBuilder.append(location);
        }

        return stringBuilder.toString();
    }

    /**
     * 校验清洗数据
     * 防止前端提交的数据有非法数据
     * @param retainList 需要保留的值集合
     * @param suitableMemberTypeList 需要清洗值的集合
     * @return 清洗后集合
     */
    public static List<Integer> checkAndClean(List<Integer> retainList, List<Integer> suitableMemberTypeList) {
        return suitableMemberTypeList.stream().filter(retainList::contains).collect(Collectors.toList());
    }

    /**
     * 解析标记
     * @param fieldLocationStr 标记
     * @param location 标记位
     * @return 返回结果
     */
    private static boolean parseFieldLocation(String fieldLocationStr, int location) {
        if (location <= 0 || fieldLocationStr.length() < location) {
            return false;
        }

        // '1' - '0' = 1, '0' - '0' = 0
        Integer value = fieldLocationStr.toCharArray()[location - 1] - '0';

        return one_char_mark.equals(value);
    }
}
